package sn.gandal.agriculture.agrimobile.j2me.gui;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;

/**
 *
 * @author sowdiomyero
 */
public class DeleteInfoForm extends Form implements CommandListener
{

    private MIDlet midlet;
    private Display display;
    private Displayable previousScreen;
    private StringItem strMessage;
    private Command cmdOk;
    private Command cmdBack;
    //private static final String EFFACER_MESSAGE = "\n L'identifiant entrée est différent.\n Par conséquent les données sur le mobile seront effacées.";
    private static final String EFFACER_MESSAGE = "\n Vous êtes connecté.";
    private IdentificationForm identificationForm;

    public DeleteInfoForm(Display display, Displayable previous, IdentificationForm form)
    {
	//super("Effacer Données");
	super("ADP connecté");

	this.display = display;
	this.previousScreen = previous;
	this.identificationForm = form;

	this.initialiseComponents();
	this.addComponents();
	this.initialiseCommands();
	this.addCommands();

	this.setCommandListener(this);
    }

    private void initialiseComponents()
    {
	this.strMessage = new StringItem("", DeleteInfoForm.EFFACER_MESSAGE);
    }

    private void addComponents()
    {
	this.append(this.strMessage);
    }

    private void initialiseCommands()
    {
	this.cmdOk = new Command("Ok", Command.OK, 1);
	this.cmdBack = new Command("Back", Command.BACK, 2);
    }

    private void addCommands()
    {
	this.addCommand(this.cmdOk);
	this.addCommand(this.cmdBack);
    }

    public void effacerAction()
    {
	//Supprimer toutes les données existant	
	this.getIdentificationForm().deleteInfo();

	//Sauvegarder le nouveau AgentDePesee	
	this.getIdentificationForm().saveNewAdp();
    }

    public void cancelAction()
    {
	this.display.setCurrent(this.previousScreen);
	//this.display.setCurrent(this);
    }

    public void commandAction(Command c, Displayable d)
    {
	if (c.equals(this.cmdOk))
	{
	    this.effacerAction();
	}
	else if (c.equals(this.cmdBack))
	{
	    this.cancelAction();
	}
    }

    private IdentificationForm getIdentificationForm()
    {
	if (this.identificationForm == null)
	{
	    this.identificationForm = new IdentificationForm(this.midlet, this.display);
	}

	return this.identificationForm;
    }
}
